package com.chixing.service.impl;

import com.alibaba.fastjson2.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.chixing.service.MsmService;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public Boolean send(Map<String, Object> param, String phone) {

        DefaultProfile profile = DefaultProfile.getProfile("default","LTAI5tGdBNY8rKfzUsYLds8Z","Xqa5JRcoMbpnzGv0JD0pZKwRKwyG2U");
        IAcsClient client = new DefaultAcsClient(profile);
        
        //设置固定参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //设置发送相关参数
        request.putQueryParameter("PhoneNumbers",phone); //手机号
        request.putQueryParameter("SignName","阿里云短信测试"); //签名
        request.putQueryParameter("TemplateCode","SMS_154950909"); //模板

        System.out.println("验证码数据，转为 json格式：" + JSON.toJSONString(param));
        request.putQueryParameter("TemplateParam", JSON.toJSONString(param)); //验证码数据，转为 json格式

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }

    }
}
